﻿using System;
using System.Xml;

namespace yunio
{
    using yunio._dat_xml;
    public class YunioXml
    {
        XmlDocument xml { get; set; }
        XmlNamespaceManager ns;
        public YunioXml()
        {
            xml = new XmlDocument();
            ns = new XmlNamespaceManager(xml.NameTable);
            xmlhelper.load_ns(xml, ref ns);
        }
        public YunioXml(string xml_text)
        {
            xml = new XmlDocument();
            xml.LoadXml(xml_text);
            ns = new XmlNamespaceManager(xml.NameTable);
            xmlhelper.load_ns(xml, ref ns);
        }
        internal YunioXml(XmlNode node)
        {
            xml = new XmlDocument();
            xml.AppendChild(node);
            ns = new XmlNamespaceManager(xml.NameTable);
            xmlhelper.load_ns(xml, ref ns);
        }
        internal XmlNode _find(string path)
        {
            var node = xml.SelectSingleNode(path, ns);
            return node;
        }
        public YunioXml Find(string xpath)
        {
            var item = _find(xpath);
            if (item != null) return new YunioXml(item);
            return new YunioXml();
        }
        public string Text(string xpath)
        {
            var item = _find(xpath);
            if(item != null) return item.InnerText;
            return string.Empty;
        }
        public string AttrText(string xpath, string attr)
        {
            var item = _find(xpath);
            if(item != null)
            foreach (XmlAttribute i in item.Attributes) if (i.Name == attr) return i.Value;
            return string.Empty;
        }


    }
}
